package com.xoa.service.vehicle;

import com.xoa.dao.common.SysCodeMapper;
import com.xoa.dao.vehicle.VehicleMaintenanceMapper;
import com.xoa.dao.vehicle.VehicleMapper;
import com.xoa.model.common.SysCode;
import com.xoa.model.vehicle.Vehicle;
import com.xoa.model.vehicle.VehicleMaintenanceWithBLOBs;
import com.xoa.model.vehicle.VehicleWithBLOBs;
import com.xoa.util.ExcelUtil;
import com.xoa.util.ToJson;
import com.xoa.util.common.log.FileUtils;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.OutputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 创建作者:   高亚峰
 * 创建日期:   2017/9/20 15:59
 * 类介绍  :   车辆维护
 * 构造参数:
 */
@Service
public class VehicleMaintenanceService {

    @Autowired
    VehicleMaintenanceMapper vehicleMaintenanceMapper;

    @Autowired
    VehicleMapper vehicleMapper;

    @Autowired
    SysCodeMapper sysCodeMapper;

    /**
     * 创建作者:   高亚峰
     * 创建日期:   2017/9/20 16:09
     * 类介绍  :   增加车辆维护记录
     * 构造参数:
     */
    public ToJson<Object> addMaintenance(VehicleMaintenanceWithBLOBs vehicleMaintenanceWithBLOBs){
        ToJson<Object> json =new ToJson<Object>(1,"err");
        try {
            int i = vehicleMaintenanceMapper.insertSelective(vehicleMaintenanceWithBLOBs);
            if(i>0){
                json.setMsg("ok");
                json.setFlag(0);
            }
        } catch (Exception e) {
            json.setMsg(e.getMessage());
            e.printStackTrace();
        }
        return json;

    }
    /**
     * 创建作者:   高亚峰
     * 创建日期:   2017/9/20 16:09
     * 类介绍  :   车辆维护记录查询导出
     * 构造参数:
     */
    public ToJson<VehicleMaintenanceWithBLOBs> selectAllMaintenance(HttpServletRequest request, HttpServletResponse response,VehicleMaintenanceWithBLOBs vehicleMaintenanceWithBLOBs, String startTime, String endTime, String startFee, String endFee, String export){
        ToJson<VehicleMaintenanceWithBLOBs> json =new ToJson<VehicleMaintenanceWithBLOBs>(1,"err");
        try {
            if(export==null){
                export="0";
            }
            Map<String, Object> map = new HashMap<String, Object>();
            map.put("startTime", startTime);
            map.put("endTime", endTime);
            map.put("vehicleMaintenanceWithBLOBs", vehicleMaintenanceWithBLOBs);
            List<VehicleMaintenanceWithBLOBs> vehicleMaintenanceWithBLOBs1 = vehicleMaintenanceMapper.selectAllMaintenance(map);
           for(VehicleMaintenanceWithBLOBs vehicleMaintenanceWithBLOBs2:vehicleMaintenanceWithBLOBs1){
               if(vehicleMaintenanceWithBLOBs2.getvId()!=null && vehicleMaintenanceWithBLOBs2.getvId()!=""){
                   VehicleWithBLOBs vehicleWithBLOBs = vehicleMapper.selectByPrimaryKey(Integer.valueOf(vehicleMaintenanceWithBLOBs2.getvId()));
                   //设置车牌号
                   if(vehicleWithBLOBs!=null){
                       vehicleMaintenanceWithBLOBs2.setvIdNumber(vehicleWithBLOBs.getvNum());
                   }
               }

               //设置维护类型
               SysCode sysCode = sysCodeMapper.getSingleCode("FIX_TYPE", vehicleMaintenanceWithBLOBs2.getVmType());
               if(sysCode!=null){
                   vehicleMaintenanceWithBLOBs2.setVmTypeName(sysCode.getCodeName());
               }
           }
            json.setFlag(0);
            json.setMsg("ok");
            json.setObj(vehicleMaintenanceWithBLOBs1);
            //判断是否是导出
            if (export.equals("1")) {
                HSSFWorkbook workbook1 = ExcelUtil.makeExcelHead("车辆维护记录表", 9);
                String[] secondTitles = {"车牌号", "维护日期", "维护类型", "维护原因", "维护费用", "经办人", "备注"};
                HSSFWorkbook workbook2 = ExcelUtil.makeSecondHead(workbook1, secondTitles);
                String[] beanProperty = {"vIdNumber", "vmRequestDate", "vmTypeName", "vmReason", "vmFee", "vmPerson", "vmRemark"};
                HSSFWorkbook workbook = null;
                workbook = ExcelUtil.exportExcelData(workbook2, vehicleMaintenanceWithBLOBs1, beanProperty);
                OutputStream out = response.getOutputStream();
                String filename = "车辆维护记录表.xls";
                filename = FileUtils.encodeDownloadFilename(filename, request.getHeader("user-agent"));
                response.setContentType("application/vnd.ms-excel;charset=UTF-8");
                response.setHeader("content-disposition", "attachment;filename=" + filename);
                workbook.write(out);
                out.close();
            }
        }catch (Exception e) {
            json.setMsg(e.getMessage());
            e.printStackTrace();
        }
        return json;
    }
    /**
     * 创建作者:   高亚峰
     * 创建日期:   2017/9/20 18:19
     * 类介绍  :   车辆维护信息修改
     * 构造参数:
     */
    public ToJson<Object> editMaintenance(VehicleMaintenanceWithBLOBs vehicleMaintenanceWithBLOBs){
        ToJson<Object> json =new ToJson<Object>(1,"err");
        try {
            int i = vehicleMaintenanceMapper.updateByPrimaryKeySelective(vehicleMaintenanceWithBLOBs);
            json.setMsg("ok");
            json.setFlag(0);
        } catch (Exception e) {
            json.setMsg(e.getMessage());
            e.printStackTrace();
        }
        return json;
    }

    /**
     * 创建作者:   高亚峰
     * 创建日期:   2017/9/20 18:19
     * 类介绍  :   车辆维护信息删除
     * 构造参数:
     */
    public ToJson<VehicleMaintenanceWithBLOBs> deleteMaintenance(Integer vmId){
        ToJson<VehicleMaintenanceWithBLOBs> json =new ToJson<VehicleMaintenanceWithBLOBs>(1,"err");
        try {
            int i = vehicleMaintenanceMapper.deleteByPrimaryKey(vmId);
            json.setMsg("ok");
            json.setFlag(0);
        } catch (Exception e) {
            json.setMsg(e.getMessage());
            e.printStackTrace();
        }
        return json;
    }
    /**
     * 创建作者:   高亚峰
     * 创建日期:   2017/9/26 18:19
     * 类介绍  :   车辆详细信息查询
     * 构造参数:
     */
    public ToJson<VehicleMaintenanceWithBLOBs> getdetail(Integer vmId){
        ToJson<VehicleMaintenanceWithBLOBs> json =new ToJson<VehicleMaintenanceWithBLOBs>(1,"err");
        try {
            VehicleMaintenanceWithBLOBs vehicleMaintenanceWithBLOBs = vehicleMaintenanceMapper.selectByPrimaryKey(vmId);
            json.setMsg("ok");
            json.setFlag(0);
            json.setObject(vehicleMaintenanceWithBLOBs);
        } catch (Exception e) {
            json.setFlag(1);
            json.setMsg(e.getMessage());
            e.printStackTrace();
        }
        return json;

    }
}
